<html>
<head>
    <title>Resultados</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
require 'functions.php';



$ZONA_QUERY = "SELECT zona_id FROM `zona`";
$ZONAS = sqlQueryToDB($ZONA_QUERY);

$QUERY = "SELECT DISTINCT maquina.`nombre` AS Maquina, maquina.`marca` AS Marca
          FROM maquina
          WHERE";
$firstZone= 1;
foreach($ZONAS as $ITEM) {
    if($firstZone){
        $firstZone = 0;
    }else{
        $QUERY .=" AND ";
    }
    $QUERY .= "maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= ".$ITEM["zona_id"].")";
}
$QUERY .= "GROUP BY maquina.`maquina_id` ";


/*
$QUERY = "SELECT DISTINCT maquina.`nombre` AS Maquina, maquina.`marca` AS Marca
FROM maquina
WHERE maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 1) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 2) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 3) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 4) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 5) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 6) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 7) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 8) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 9) AND
      maquina.`maquina_id` IN (select maquina.`maquina_id`
                               from maquina
                                 inner join maquinacliente on maquinacliente.`maquina_id`=maquina.`maquina_id`
                                 inner join cliente on maquinacliente.`cliente_id`=cliente.`cliente_id`
                                 inner join zona on zona.`zona_id`=cliente.`zona_id`
                               where zona.`zona_id`= 10)
GROUP BY maquina.`maquina_id` ";
*/

/*Muestra la consulta bonita*/
sqlQueryPrettyParser($QUERY);

/*Efectua la consulta*/
$DATA = sqlQueryToDB($QUERY);

/*Muestra la tabla de resultados*/
if ($DATA->num_rows < 1) {
    echo "No hay resultados para armar una tabla<br><br>";
} else {
    echo sqlDataToHtmlTable($DATA);
}

?>
<br>
<input type="button" onclick="history.back()" name="Regresar" value="Regresar">
</body>
</html>
